package com.property.manage.service.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.property.manage.dao.system.RoleMenuMapper;
import com.property.manage.model.system.RoleMenu;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 描述
 *
 * @author lwb
 * @since 2020/7/15
 */
@Component
@Slf4j
@AllArgsConstructor
public class RoleMenuService extends ServiceImpl<RoleMenuMapper, RoleMenu> {

    private RoleMenuMapper roleMenuMapper;

    public List<RoleMenu> getRoleMenus(Integer roleId){

        QueryWrapper<RoleMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleId);
        return roleMenuMapper.selectList(wrapper);
    }

    public void deleteRoleMenu(Integer roleId){
        UpdateWrapper<RoleMenu> wrapper = new UpdateWrapper<>();
        wrapper.eq("role_id", roleId);
        roleMenuMapper.delete(wrapper);
    }

    public void batchInsert(Integer roleId, Collection<Integer> menuIds){
        if(CollectionUtils.isEmpty(menuIds)){
            return;
        }

        List<RoleMenu> roleMenus = menuIds.stream().map(menuId -> {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setMenuId(menuId);
            roleMenu.setRoleId(roleId);
            return roleMenu;
        }).collect(Collectors.toList());

        saveBatch(roleMenus);
    }
}
